/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.windows.attackWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import de.cbf.cam.dsa.model.entities.BattleChar;
import de.cbf.cam.dsa.model.entities.constants.ManeuverConstants;
import de.cbf.cam.dsa.model.entities.maneuvers.OneManeuver;

/**
 * @author Nebelritter
 *
 */
public class SideDefender extends Composite {

	/**
	 * @uml.property  name="textState"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Text textState;
	
	/**
	 * @uml.property  name="defenderSelection"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> defenderSelection = new ArrayList<ActionListener>();
	/**
	 * @uml.property  name="defenderManeuverSelection"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> defenderManeuverSelection = new ArrayList<ActionListener>();

	/**
	 * @uml.property  name="tableDefenderManeuvers"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.maneuvers.OneManeuver"
	 */
	private Table tableDefenderManeuvers;

	/**
	 * @uml.property  name="tableDefenders"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Table tableDefenders;
	
	
	private static final Color GRAY =Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);

	/**
	 * @param parent
	 * @param style
	 */
	public SideDefender(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout2f =new GridLayout(2,false);
		layout2f.marginBottom = 3;
		layout2f.marginTop	  = 1;
		layout2f.marginLeft   = 3;
		layout2f.marginRight  = 3;
		layout2f.marginWidth  = 0;
		layout2f.marginHeight = 0;
		setLayout(layout2f);
		
		tableDefenders = new Table(this, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.SINGLE|SWT.CHECK|SWT.FULL_SELECTION);
		tableDefenders.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,2));
		tableDefenders.setHeaderVisible(true);
		tableDefenders.setLinesVisible(true);
		tableDefenders.addSelectionListener(new TableDefenderSelection());
		
			TableColumn column = new TableColumn(tableDefenders, SWT.LEFT);
			column.setText("!");
			column.setWidth(20);
			
			column = new TableColumn(tableDefenders,SWT.CENTER);
			column.setText("Name");
			column.setWidth(80);
			
			column = new TableColumn(tableDefenders, SWT.LEFT);
			column.setText("Beschreibung");
			column.setWidth(200);
			
			column = new TableColumn(tableDefenders, SWT.CENTER);
			column.setText("Entf.");
			column.setWidth(40);
			
		Label labelStaticState = new Label(this, SWT.NONE);
		labelStaticState.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		labelStaticState.setText("Status:");
		
		textState = new Text(this, SWT.BORDER|SWT.READ_ONLY);
		textState.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
		
		tableDefenderManeuvers = new Table(this, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.MULTI|SWT.CHECK);
		tableDefenderManeuvers.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,2));
		tableDefenderManeuvers.setHeaderVisible(true);
		tableDefenderManeuvers.setLinesVisible(true);		
		tableDefenderManeuvers.addMouseListener(new TableOptionsSelection());
		
			column = new TableColumn(tableDefenderManeuvers, SWT.LEFT);
			column.setText("!");
			column.setWidth(20);
			
			column = new TableColumn(tableDefenderManeuvers, SWT.LEFT);
			column.setText("Option");
			column.setWidth(150);
			
			column = new TableColumn(tableDefenderManeuvers, SWT.CENTER);
			column.setText("Wert");
			column.setWidth(40);
	}
	
	public void setDefenderManeuvers(List<OneManeuver> selected, List<OneManeuver> enabled,List<OneManeuver> disabled){
		tableDefenderManeuvers.removeAll();
		addDefenderManeuvers(selected,ManeuverConstants.STATE_SELECTED);
		addDefenderManeuvers(enabled ,ManeuverConstants.STATE_ENABLED);
		addDefenderManeuvers(disabled,ManeuverConstants.STATE_DISABLED);
	}
	
	private void addDefenderManeuvers(List<OneManeuver> maneuvers,int state) {		
		for(int i =0;i<maneuvers.size();i++){
			OneManeuver maneuver = maneuvers.get(i);
			TableItem item = new TableItem(tableDefenderManeuvers, SWT.NONE);
			item.setText(0, "");
			item.setText(1,maneuver.getManeuverName());
			if(maneuver.getMinModifier()>=0){
				item.setText(2,maneuver.getMinModifier().toString());
			}				
			item.setData(maneuver);			
			switch (state) {
				case ManeuverConstants.STATE_DISABLED:{
					item.setGrayed(true);
					item.setForeground(GRAY);
				break;
				}
				case ManeuverConstants.STATE_SELECTED:{
					item.setChecked(true);
				break;
				}
			}
		}		
	}
	
	
	public void addManeuverItem(OneManeuver maneuver,boolean checked,boolean enabled){
		TableItem item = new TableItem(tableDefenderManeuvers, SWT.NONE);
		item.setText(0, "");
		item.setText(1,maneuver.getManeuverName());
		if(maneuver.getMinModifier()>=0){
			item.setText(2,maneuver.getMinModifier().toString());
			//make modificator column not editable
			
		}		
		item.setChecked(checked);
		if(!enabled){
			item.setGrayed(true);
			item.setForeground(GRAY);
		}		
		item.setData(maneuver);
	}
	
	
	public BattleChar getSelectedDefender(){
		if(tableDefenders.getSelectionCount()>0){
			return (BattleChar)tableDefenders.getSelection()[0].getData();
		}
		return null;
	}
	
	public List<OneManeuver> getDefenderSelectedManeuvers(){
		List<OneManeuver> result = new ArrayList<OneManeuver>();
		for(int i =0;i<tableDefenderManeuvers.getItemCount();i++){
			TableItem item =tableDefenderManeuvers.getItem(i);
			if(item.getChecked()){
				result.add((OneManeuver)item.getData());
			}
		}
		return result;
	}
	
	public List<OneManeuver> getAllDefenderManeuvers(){
		List<OneManeuver> result = new ArrayList<OneManeuver>();
		for(int i =0;i<tableDefenderManeuvers.getItemCount();i++){
			TableItem item =tableDefenderManeuvers.getItem(i);			
			result.add((OneManeuver)item.getData());			
		}
		return result;
	}
		
	public void setDefenders(List<BattleChar> defenders){
		tableDefenders.removeAll();
		for(int i =0;i<defenders.size();i++){
			TableItem item = new TableItem(tableDefenders, SWT.NORMAL);
			BattleChar enemy = defenders.get(i);
			item.setText(1,enemy.getCharacter().getName());
			item.setText(2,enemy.getWoundsDescription());
						
			item.setData(enemy);				
		}	
		if(defenders.size()>0){
			tableDefenders.select(0);				
			//launch Event
			Event event = new Event();
			tableDefenders.notifyListeners(SWT.Selection, event);
		}
	}
	
	public void setDefenderState(String state){
		textState.setText(state);
	}
	
	public void setCurrentItemChecked(){
		tableDefenders.getSelection()[0].setChecked(true);
	}
	
	public boolean registerTableDefenderSelectionListener(ActionListener listener){
		return defenderSelection.add(listener);
	}	
	public boolean registerTableDefenderManeuverSelectionListener(ActionListener listener){
		return defenderManeuverSelection.add(listener);
	}
	public boolean deregisterTableDefenderSelectionListener(ActionListener listener){
		return defenderSelection.remove(listener);
	}
	public boolean deregisterTableDefenderOptionsSelectionListener(ActionListener listener){
		return defenderManeuverSelection.remove(listener);
	}
	
	private class TableDefenderSelection extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent arg0) {
			ActionEvent event = new ActionEvent(SideDefender.this, ActionEvent.ACTION_PERFORMED,"defendersSelected");
			for (ActionListener actionListener : defenderSelection) {
				actionListener.actionPerformed(event);
			}
		}
		
	}
	
	private class TableOptionsSelection extends MouseAdapter{
		@Override
		public void mouseDown(MouseEvent e) {
			//only handle events for "enabled"(non grayed Items)
			TableItem item =tableDefenderManeuvers.getItem(new Point(e.x,e.y));
			if(item!=null){
				if(item.getGrayed()){
					item.setChecked(false);
				}else{
					ActionEvent event = new ActionEvent(SideDefender.this, ActionEvent.ACTION_PERFORMED,"defenderOptionSelected");
					for (ActionListener actionListener : defenderManeuverSelection) {
						actionListener.actionPerformed(event);
					}
				}	
			}			
		}				
	}
}
